package com.linkai.controller.admin.info;


import com.linkai.controller.admin.BaseController;
import com.linkai.entity.Customer;
import com.linkai.enums.ResultDTO;
import com.linkai.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yamon
 * @since 2021-03-09
 */
@Controller
@RequestMapping("/admin/info/customer")
public class CustomerController extends BaseController implements InfoBaseController{

    @Autowired
    private CustomerService customerService;

    @GetMapping("/index")
    public ModelAndView index(){
        final ModelAndView modelAndView = new ModelAndView();
        final List<Customer> list = customerService.list();
        modelAndView.addObject("customer",list);
        modelAndView.setViewName("/admin/info/customer/index");
        return modelAndView;
    }

    /**
     * 跳转增加页面
     * @return list
     */
    @GetMapping("/add")
    public String add(){
        return "admin/info/customer/add";
    }
    /**
     * 跳转到页面，修改页面并获取数据
     * @param id id
     * @return page
     */
    @GetMapping("/add/{id}")
    public ModelAndView add(@PathVariable Integer id){
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customer",customerService.getById(id));
        modelAndView.setViewName("admin/info/customer/add");
        return modelAndView;
    }
    /**
     * 保存或者修改一个职员信息
     * @return BOOLEAN
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ResultDTO<Boolean> addEmployee(@RequestBody Customer customer){
        if (customer.getId()!=null){
            //更新
            return super.updateInfo(customerService.updateById(customer));
        }
        return super.successSaveInfo(customerService.save(customer));
    }

    /**
     * 根据id得到实体类
     * @param id id
     * @return 实体类
     */
    @GetMapping("/get/{id}")
    public Customer getOneEmployee(@PathVariable Integer id){
        return customerService.getById(id);
    }



    /**
     * 根据id删除所有职员信息
     * @param id id
     * @return flag
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultDTO<Boolean> deleteOneEmployee(@PathVariable Integer id){
        return super.successDeleteInfo(customerService.removeById(id));
    }


    @Override
    public ResultDTO<Boolean> saveSize(String sizeDesc) {
        return null;
    }

    @Override
    @GetMapping("/list")
    @ResponseBody
    public List<Customer> listAll() {
        return customerService.list();
    }

}

